// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/gdax/api_util/auth_key.h"

#include "coin2/base/base64.h"
#include "coin2/base/time.h"
#include "coin2/exchange/base/api_util/api_signature.h"
#include "coin2/exchange/base/api_util/json_writer.h"

std::string GdaxBaseAuth::GetCurrTimeSecondsThreeDecimal() {
  std::string ts_str = std::to_string(GetCurrentTimestamp() / 1000000);
  ts_str.insert(ts_str.end() - 3, 1, '.');
  return ts_str;
}

std::string GdaxBaseAuth::GetSignature(
    const std::string& secret_key,
    const std::string& address,
    const std::string& method,
    const std::string& timestamp,
    const std::string& param) {
  if (secret_key.empty()) {
    throw std::runtime_error("[gdax] secret key is empty");
  }
  std::string cre = timestamp + method + address + param;
  return ApiSignatureUtil::GetSignatureByHmacSHA256(cre, impl::base64_decode(secret_key));
}

std::list<std::string> GdaxWsAuth::GetSignatureStringList() const {
  std::list<std::string> out;
  std::string ts_str = GetCurrTimeSecondsThreeDecimal();
  std::string signa = GetSignature(key_.secret_key(), "/users/self/verify", "GET", ts_str, "");
  out.emplace_back(key_.access_key());
  out.emplace_back(key_.GetValue("passphrase").value());
  out.emplace_back(ts_str);
  out.emplace_back(signa);
  return out;
}

std::vector<std::pair<std::string, std::string>> GdaxRestAuth::GetHeaderSet(
    const std::string& address,
    const std::string& method,
    const std::string& param) const {
  std::vector<std::pair<std::string, std::string>> out;
  std::string ts_str = GetCurrTimeSecondsThreeDecimal();
  std::string signa = GetSignature(key_.secret_key(), address, method, ts_str, param);
  out.push_back(std::make_pair("CB-ACCESS-KEY", key_.access_key()));
  out.push_back(std::make_pair("CB-ACCESS-SIGN", signa));
  out.push_back(std::make_pair("CB-ACCESS-TIMESTAMP", ts_str));
  out.push_back(std::make_pair("CB-ACCESS-PASSPHRASE", key_.GetValue("passphrase").value()));
  return out;
}

AsioHttpContext* GdaxRestAuth::CreateRequestByGetWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  std::string query_string = builder->ComputeQueryString();
  CHECK_THROW(builder->ComputePostBody().empty()) << "It is GET Method, No Payload";
  std::string target = address;
  if (!query_string.empty()) {
    target += "?" + query_string;
  }
  auto ahc =
      AsioHttpContext::NewGet(host_, port_, bind_ip_, target, GetHeaderSet(target, "GET", ""));
  ahc->keep_user_agent = true;
  return ahc;
}

AsioHttpContext* GdaxRestAuth::CreateRequestByPostWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  std::string query_string = builder->ComputeQueryString();
  std::string post_body = builder->ComputePostBody();
  std::string target = address;
  if (!query_string.empty()) {
    target += "?" + query_string;
  }
  auto ahc = AsioHttpContext::NewPost(
      host_,
      port_,
      bind_ip_,
      target,
      GetHeaderSet(target, "POST", post_body),
      post_body);
  ahc->keep_user_agent = true;
  return ahc;
}

AsioHttpContext* GdaxRestAuth::CreateRequestByDeleteWithSignature(
    const std::string& address,
    UrlParamsBuilder* builder) {
  std::string query_string = builder->ComputeQueryString();
  std::string post_body = builder->ComputePostBody();
  std::string target = address;
  if (!query_string.empty()) {
    target += "?" + query_string;
  }
  auto ahc = AsioHttpContext::NewDelete(
      host_,
      port_,
      bind_ip_,
      target,
      GetHeaderSet(target, "DELETE", post_body),
      post_body);
  ahc->keep_user_agent = true;
  return ahc;
}
